package hk.com.fgoproduction.getdroplets.Lib.FileMan

import android.content.Context
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.CheckBox
import android.widget.TextView
import hk.com.fgoproduction.getdroplets.R
import java.util.*

class ModAdapter : BaseAdapter {

    //http://blog.csdn.net/u012457196/article/details/38304801?utm_source=tuicool
    //http://blog.csdn.net/nihaoqiulinhe/article/details/49021169
    private var list = ArrayList<String>()
    private var isSelected = SparseBooleanArray()
    private var ctx: Context? = null
    private var inflater: LayoutInflater? = null
    private var layout: Int = 0
    private var isCheck: Boolean = false

    constructor(list: ArrayList<String>, ctx: Context, listLayout: Int, isCheck: Boolean) {
        this.list = list
        this.ctx = ctx
        inflater = LayoutInflater.from(ctx)
        layout = listLayout
        this.isCheck = isCheck
        initList()
    }

    private fun initList() {
        for (i in 0..list.size) {
            isSelected.put(i, false)
        }
        //TODO check sharedpreference and init???
    }

    override fun getItem(position: Int): Any? {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        val holder: ViewHolder
        var view = convertView

        if (view === null) {
            view = LayoutInflater.from(ctx).inflate(layout, null)


            if (layout == R.layout.file_man_view_item_w_check) {
                holder = ViewHolder(view, R.id.list_text, R.id.list_check_box)
                holder.checkBox = view.findViewById(R.id.list_check_box) as CheckBox
            } else {
                holder = ViewHolder(view, R.id.list_text)
            }

            for (i in 0..count)
                isSelected.put(i, false)

        } else {
            holder = view.tag as ViewHolder
            holder.checkBox = if (isCheck) view.findViewById(R.id.list_check_box) as CheckBox else null
        }

        holder.textView.text = list[position]
        holder.checkBox?.isChecked = getIsSelected().get(position)

        view?.tag = holder
        return view
    }

    fun getIsSelected(): SparseBooleanArray {
        return isSelected
    }

    fun setIsSelected(isSelected: SparseBooleanArray) {
        this.isSelected = isSelected
    }

    class ViewHolder(view: View, textViewId: Int, checkBoxId: Int = 0) {
        var textView: TextView = view.findViewById(textViewId) as TextView
        var checkBox: CheckBox? = if (checkBoxId == 0) null else view.findViewById(checkBoxId) as CheckBox
    }


    fun notIsSelected(position: Int) {
        isSelected.put(position, !isSelected.get(position))
    }

    fun toggle() {

    }
}